package com.nercel.dsj.gksales.shiro.util;

import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Arrays;
import java.util.List;

import static com.nercel.dsj.gksales.constant.Const.SHIRO_FILTERS;

/**
 * @author sunlin
 * @date 2018-12-7
 */
public class ShiroUtil {

	public static String getRealPath(String path, ServletRequest request) {
		String[] paths = path.split(":");
		String realPath = paths[paths.length == 1 ? 0 : 1];
		if ((paths.length == 2 && WebUtils.toHttp(request).getMethod().toUpperCase().equals(paths[0].toUpperCase()))
				|| paths.length == 1) {
			return realPath;
		}
		return null;
	}

	/**
	 * 检测权限配置
	 * @param config
	 * @return
	 */
	public static boolean checkPermssionsConfig(String config) {
		if (config == null || config.equals("")) {
			return false;
		}
		String[] filters = config.replaceAll("\\[.*?]", "").split(",");
		List<String> list = Arrays.asList(SHIRO_FILTERS);
		for (String filter : filters) {
			if (list.indexOf(filter) == -1) {
				return false;
			}
		}
		return true;
	}


}
